package com.lcy.reggie.controller;

import com.lcy.reggie.common.Result;
import com.lcy.reggie.entity.AddressBook;
import com.lcy.reggie.service.AddressBookService;
import com.lcy.reggie.utils.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ======刘春春大帅逼======
 * ******java学习之旅******
 */


/**
 * 地址簿管理
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查看收货地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        return addressBookService.listAddressBook();
    }


    /**
     * 新增收货地址
     */
    @PostMapping()
    public Result<String> saveAddress(@RequestBody AddressBook addressBook){
        return addressBookService.saveAddress(addressBook);
    }

    /**
     * 设置默认收货地址
     */
    @PutMapping("/default")
    public Result<AddressBook> updateDefault(@RequestBody AddressBook addressBook){
        return addressBookService.updateDefault(addressBook);
    }

    /**
     * 修改回显
     */
    @GetMapping("/{id}")
    public Result<AddressBook> selectById(@PathVariable Long id){
        return addressBookService.selectById(id);
    }


    /**
     * 修改地址信息
     */
    @PutMapping()
    public Result<String> updateAddress(@RequestBody AddressBook addressBook){
        return addressBookService.updateAddress(addressBook);
    }

    /**
     * 删除地址
     */
    @DeleteMapping()
    public Result<String> deleteAddress(@RequestParam("ids")Long id){
        addressBookService.removeById(id);
        return Result.success("删除成功！");
    }

    /**
     * 显示默认收货地址
     */
    @GetMapping("/default")
    public Result<AddressBook> queryDefault(){
        Long userId = BaseContext.getCurrentId();
        AddressBook adress = addressBookService
                .query()
                .eq("user_id", userId)
                .eq("is_default", 1)
                .one();
        return Result.success(adress);
    }

}
